package com.htht.smartschool.action.attendance;

import java.util.List;
import java.util.Map;

import com.htht.smartschool.action.baseaction.BaseAction;
import com.htht.smartschool.common.model.ParamData;
import com.htht.smartschool.common.servlet.DataContainer;
import com.htht.smartschool.model.attendance.AttendanceIntoSchoolInfo;
import com.htht.smartschool.model.attendance.IntoSchoolRecordQueryResult;
import com.htht.smartschool.model.attendance.TAttendance;
import com.htht.smartschool.model.attendance.TAttendanceRegular;
import com.htht.smartschool.model.attendance.TAttendanceStatistics;
import com.htht.smartschool.service.attendance.AttendanceService;
import com.htht.smartschool.service.attendance.ClassInfo;
import com.htht.smartschool.util.system.GsonUtils;
import com.htht.smartschool.util.system.ResultSetToExcel;

public class AttendanceAction extends BaseAction{

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean processData(DataContainer obj, ParamData pd) {
		AttendanceService attendanceService = new AttendanceService();
		boolean flag = false;
		TAttendance attendance = null;
		try {
			attendance = (TAttendance) GsonUtils.getInstance().fromJson(pd.getData(), TAttendance.class);
		} catch (Exception e) {
		}
		if(pd.getCmd().equalsIgnoreCase("addAttendance")){
			boolean result = attendanceService.addAttendance(attendance);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加考勤失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addAttendanceStatistics")){
			TAttendanceStatistics tattendancestatistics =  (TAttendanceStatistics) 
					GsonUtils.getInstance().fromJson(pd.getData(), TAttendanceStatistics.class);
			boolean result = attendanceService.addAttendanceStatistics(tattendancestatistics);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加考勤统计失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateAttendance")){
			boolean result = attendanceService.updateAttendance(attendance);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改考勤失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteAttendance")){
			boolean result = attendanceService.deleteAttendance(attendance);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除考勤失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAttendance")){
			List<TAttendance> result = attendanceService.getAttendance(attendance);
			if(result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAttendanceByPage")){
			Map map = attendanceService.getAttendanceByPage(attendance);//查询学生考勤数据
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAttendanceByClasscode")){
			List<TAttendanceStatistics> result = attendanceService.getAttendanceByClasscode(attendance);
			if(result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAllClass")){
			List<ClassInfo> result = attendanceService.getAllClass();
			if(result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAttendanceByClasscodeByPage")){
			Map map = attendanceService.getAttendanceByClasscodeByPage(attendance);
			List<TAttendance> attend = (List<TAttendance>) map.get("list");
			ResultSetToExcel rse = (ResultSetToExcel) map.get("remark");
			if (attend.size()>0) {
				flag = true;
				obj.setData(attend);
				if(rse != null){
					obj.setRemark(rse);
				}
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryStudentInfo")){
			AttendanceIntoSchoolInfo attendanceIntoSchoolInfo = (AttendanceIntoSchoolInfo) GsonUtils.getInstance().fromJson(pd.getData(), AttendanceIntoSchoolInfo.class);
			List<AttendanceIntoSchoolInfo> list =  attendanceService.getStudentInfo(attendanceIntoSchoolInfo);//查询学生的info
			if(list.size()>0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateAttendanceRegular")){
			TAttendanceRegular attendanceregular =(TAttendanceRegular) GsonUtils.getInstance().fromJson(pd.getData(), TAttendanceRegular.class);
			boolean result = attendanceService.updateAttendanceRegular(attendanceregular);//更新考勤规则
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("更新失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("deleteAttendanceRegular")){
			TAttendanceRegular attendanceregular =(TAttendanceRegular) GsonUtils.getInstance().fromJson(pd.getData(), TAttendanceRegular.class);
			boolean result = attendanceService.deleteAttendanceRegular(attendanceregular);//删除考勤规则
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("insertAttendanceRegular")){
			TAttendanceRegular attendanceregular =(TAttendanceRegular) GsonUtils.getInstance().fromJson(pd.getData(), TAttendanceRegular.class);
			boolean result = attendanceService.insertAttendanceRegular(attendanceregular);//添加考勤规则
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("queryAttendanceRegular")){
			TAttendanceRegular attendanceregular =(TAttendanceRegular) GsonUtils.getInstance().fromJson(pd.getData(), TAttendanceRegular.class);
			Map map = attendanceService.queryAttendanceRegular(attendanceregular);//查询考勤规则
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查询角色用户失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryAttendanceByAdmin")){
			AttendanceIntoSchoolInfo attendanceIntoSchoolInfo = (AttendanceIntoSchoolInfo) GsonUtils.getInstance().fromJson(pd.getData(), AttendanceIntoSchoolInfo.class);
			Map map = attendanceService.queryAttendanceByAdmin(attendanceIntoSchoolInfo);//查询考勤规则
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("管理员查询考勤统计失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryAttendanceByTeacher")){
			AttendanceIntoSchoolInfo attendanceIntoSchoolInfo = (AttendanceIntoSchoolInfo) GsonUtils.getInstance().fromJson(pd.getData(), AttendanceIntoSchoolInfo.class);
			Map map = attendanceService.queryAttendanceByTeacher(attendanceIntoSchoolInfo);//查询考勤规则
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("教师查询考勤统计失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryAttendanceByTeacherApp")){
			AttendanceIntoSchoolInfo attendanceIntoSchoolInfo = (AttendanceIntoSchoolInfo) GsonUtils.getInstance().fromJson(pd.getData(), AttendanceIntoSchoolInfo.class);
			Map map = attendanceService.queryAttendanceByTeacherApp(attendanceIntoSchoolInfo);//查询考勤规则
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("教师app查询考勤统计失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryIntoSchoolRecord")){
			IntoSchoolRecordQueryResult isrqr = (IntoSchoolRecordQueryResult) GsonUtils.getInstance().fromJson(pd.getData(), IntoSchoolRecordQueryResult.class);
			Map map = attendanceService.queryIntoSchoolRecord(isrqr);//查询考勤规则
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("进离校记录查询失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryAttendanceForUser")){
			IntoSchoolRecordQueryResult isrqr = (IntoSchoolRecordQueryResult) GsonUtils.getInstance().fromJson(pd.getData(), IntoSchoolRecordQueryResult.class);
			Map map = attendanceService.queryAttendanceForUser(isrqr);//查询考勤规则
			if(map.keySet().size() > 0){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("家长查询考勤统计失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryAttendanceForUserWeb")){
			IntoSchoolRecordQueryResult isrqr = (IntoSchoolRecordQueryResult) GsonUtils.getInstance().fromJson(pd.getData(), IntoSchoolRecordQueryResult.class);
			Map map = attendanceService.queryAttendanceForUserWeb(isrqr);//查询考勤规则
			if(map.keySet().size() > 0){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("家长查询考勤统计失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("AttendanceRetroactiveByStudentcode")){//考勤补签
			boolean result = attendanceService.AttendanceRetroactiveByStudentcode(attendance);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryIllegalleaveSchool")){//查询非法离校信息
			IntoSchoolRecordQueryResult isrqr = (IntoSchoolRecordQueryResult) GsonUtils.getInstance().fromJson(pd.getData(), IntoSchoolRecordQueryResult.class);
			Map map = attendanceService.queryIllegalleaveSchool(isrqr);
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("非法离校查询失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateIllegalleaveSchool")){//修改非法离校信息
			boolean result = attendanceService.updateIllegalleaveSchool(attendance);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			}
		}
		return flag;
	}

}
